package net.liteheaven.xplugin.demo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;

import net.liteheaven.conflict.ConflictUtil;
import net.liteheaven.xblib.plugin.MPermissionUtil;
import net.liteheaven.xblib.plugin.PluginKit;
import net.liteheaven.xblib.plugin.XProxyActivity;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends XProxyActivity implements View.OnClickListener{

    private static final String APK_NAME = "demoplugin-debug";
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        traceAppInfo();
        MPermissionUtil.grantIOPermissions(this);

        setContentView(R.layout.activity_main);
        findViewById(R.id.button3).setOnClickListener(this);
        editText = (EditText)findViewById(R.id.editText);
        String def = APK_NAME;
        editText.setText(def);

        findViewById(R.id.hideFix).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        findViewById(R.id.toast).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConflictUtil.showToast(MainActivity.this);
            }
        });
        //test(def);
    }

    //@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void traceAppInfo(){
        PackageManager pm = this.getPackageManager();
        try {
            ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), 0);
            System.out.println("sourceDir = " + ai.sourceDir);
            System.out.println("publicSourceDir = " + ai.publicSourceDir);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                System.out.println("splitPublicSourceDirs = " + ai.splitPublicSourceDirs);
                System.out.println("splitSourceDirs = " + ai.splitSourceDirs);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.button3:
                dynamicLoad();
                break;
        }
    }

    private void dynamicLoad(){
        //String innerApk = copy();
        //String apk = innerApk;

        String apk = editText.getText().toString();

        Intent intent = new Intent();
        intent.setComponent(new ComponentName(this, "net.liteheaven.xplugin.demoplugin.PluginHomeActivity"));
        PluginKit.getInstance().getIntentHandler().attachPlugin(apk, intent);
        startActivity(intent);
    }

    private void test(String def){
        File fDef = new File(def);
        System.out.println("" + fDef.getPath() + " exists=" + fDef.isFile());

        File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        boolean exists = f.exists();
        System.out.println("" + f.toString() + " exists=" + exists);
        File f2 = new File(f.toString() + "/" + "aaa");
        boolean result = f2.delete();
        System.out.println("delete result = " + result);
        result = f2.mkdirs();
        System.out.println("mkdirs result = " + result);
        writeTest();

//        try {
//            load();
//            Toast.makeText(this, "加载成功", Toast.LENGTH_SHORT).show();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
//        }
    }

    private boolean writeTest(){
        try {
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/writeTest.txt");
            if (!f.exists()){
                f.createNewFile();
            }
            FileOutputStream fs = new FileOutputStream(f);

            String text = "aaaaaaaaaaaaaaaaaaaaaa";
            byte[] bytes = text.getBytes();

            for (int i=0; i<10; ++i){
                fs.write(bytes, 0, bytes.length);
            }
            fs.close();
        }
        catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
